package org.jctc.els.modules.biz.controller.user;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.jctc.els.modules.biz.entity.user.User;
import org.jctc.els.modules.biz.service.user.IUserService;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.annotation.MultipartConfig;

import static org.springblade.core.cache.constant.CacheConstant.USER_CACHE;

@RestController
@RequestMapping("/user")
@NonDS
@ApiIgnore
@AllArgsConstructor
@MultipartConfig
public class UserController{

    private IUserService userService;

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string")
    })
    public R<IPage<User>> page(@ApiIgnore Query query,
                               @RequestParam(value = "account",required = false) String account,
                               @RequestParam(value = "deptId",required = false) Long deptId,
                               @RequestParam(value = "status",required = false) String status) {
        System.out.println("query"+query);
        IPage<User> pages = userService.selectUserPage(Condition.getPage(query),account,status,deptId);
        return R.data(pages);
    }

    @GetMapping("/dept/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string")
    })
    public R<IPage<User>> pageDept(@ApiIgnore Query query,
                               @RequestParam(value = "account",required = false) String account,
                               @RequestParam(value = "deptId",required = false) Long deptId,
                               @RequestParam(value = "status",required = false) String status) {
        IPage<User> pages = userService.selectDpetUserPage(Condition.getPage(query),account,status,deptId);
        return R.data(pages);
    }

    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/detail")
    public R<User> detail(@RequestParam("id") Long id) {
        User detail = userService.getById(id);
        return R.data(detail);
    }

    @ApiOperation(value = "新增用户表")
    @PostMapping("/save")
    public R save(@RequestBody User user) {
        user.setRoleId("1540588258452262914");
        if (ObjectUtil.isEmpty(user.getBaseRole())) {
            user.setBaseRole(2L);//添加默认角色
        }
        user.setStatus(1);//添加默认状态
        return R.status(userService.insert(user));
    }

    @ApiOperation(value = "修改用户表")
    @PutMapping("/edit")
    public R edit(@RequestBody User user) {
            CacheUtil.clear(USER_CACHE);
            boolean b = userService.updateUser(user);
            return R.data(b);
    }

    @ApiOperation(value = "删除用户表")
    @DeleteMapping("/remove")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(userService.deleteLogic(Func.toLongList(ids)));
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/reset/password")
    public R doResetPassword(@ApiParam(value = "id集合", required = true) @RequestParam("id") Long id) {
		boolean res = userService.resetPassword(id);
        return R.status(res);
    }

	@ApiOperation(value = "启用")
	@PutMapping("/enable")
	public R doEnableUser(@RequestBody User user) {
		boolean res = userService.enable(user.getId());

		return R.status(res);
	}

	@ApiOperation(value = "禁用")
	@PutMapping("/disabled")
	public R doDisabledUser(@RequestBody User user) {
		boolean res = userService.disabled(user.getId());

		return R.status(res);
	}

    /**
     * 修改密码
     */
    @PostMapping("/update-password")
    @ApiOperationSupport(order = 9)
    @ApiLog("修改密码")
    @ApiOperation(value = "修改密码", notes = "传入密码")
    public R updatePassword(BladeUser user, @ApiParam(value = "旧密码", required = true) @RequestParam String oldPassword,
                            @ApiParam(value = "新密码", required = true) @RequestParam String newPassword,
                            @ApiParam(value = "新密码", required = true) @RequestParam String newPassword1) {
        boolean temp = userService.updatePassword(user.getUserId(), oldPassword, newPassword, newPassword1);
        return R.status(temp);
    }
}
